package com.victualler.business.inventory.repository.impl;

import com.victualler.business.inventory.dto.GoodsModelInventoryDto;
import com.victualler.business.inventory.dto.GoodsModelInventoryScanDto;
import com.victualler.business.inventory.entity.GoodsInventoryDao;
import com.victualler.business.inventory.repository.GoodsInventoryQueryRepository;
import com.victualler.core.base.repository.impl.BaseQueryRepositoryImpl;
import com.victualler.core.common.utils.ExceptionWapperUtil;
import com.victualler.core.common.utils.HQLConverterUtil;
import com.victualler.product.goods.entity.GoodsMainDao;
import com.victualler.product.goods.entity.GoodsModelDetailDao;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @author Guoshichang
 * @function 封装用于复杂sql查询的API. 为每个实体的repository层提供便利操作
 */
@Component
public class GoodsInventoryQueryRepositoryImpl extends BaseQueryRepositoryImpl<GoodsInventoryDao,Long> implements GoodsInventoryQueryRepository {
    /**
     * 获取商品型号对应的库存
     * @param goodsId 商品id
     * @return GoodsModelInventoryDto
     */
    public List<GoodsModelInventoryDto> getGoodsModelInventory(Long goodsId) {
        StringBuffer sql = new StringBuffer();
        sql.append("SELECT model.id,model.parent_id ,model.img_id,model.name,model.contour_id,model.contour_name,model.real_size,model.size,model.spec_id,model.spec_name, ");
        sql.append(" sum(COALESCE(invy.quantity,0)) quantity ");
        sql.append(" FROM " + this.getTableName(GoodsModelDetailDao.class) + " model ");
        sql.append(" LEFT JOIN " + this.getTableName(GoodsInventoryDao.class) + " invy ON invy.goods_model_id = model.id AND invy.goods_id = model.parent_id ");
        sql.append(" WHERE model.parent_id = ? ");
        sql.append(" GROUP BY model.id,model.parent_id ,model.img_id,model.name,model.contour_id,model.contour_name,model.real_size,model.size,model.spec_id,model.spec_name ");
        try {
            return HQLConverterUtil.findVOListBySql(sql.toString(),new Object[]{goodsId},GoodsModelInventoryDto.class,this.getEntityManager());
        } catch (Exception e) {
            throw ExceptionWapperUtil.wapper(e);
        }
    }

    /**
     * 获取商品型号对应的库存
     * @param barCode 条形码
     * @return GoodsModelInventoryDto
     */
    public GoodsModelInventoryScanDto getGoodsModelInventoryScan(String barCode) {
        StringBuffer sql = new StringBuffer();
        sql.append("SELECT goods.name goodsName,goods.vender_Name,goods.vender_id,model.id,model.parent_id ,model.img_id,model.name,model.contour_id,model.contour_name,model.real_size,model.size,model.spec_id,model.spec_name, ");
        sql.append(" sum(COALESCE(invy.quantity,0)) quantity ");
        sql.append(" FROM " + this.getTableName(GoodsModelDetailDao.class) + " model ");
        sql.append(" LEFT JOIN " + this.getTableName(GoodsMainDao.class) + " goods ON goods.id = model.parent_id");
        sql.append(" LEFT JOIN " + this.getTableName(GoodsInventoryDao.class) + " invy ON invy.goods_model_id = model.id AND invy.goods_id = model.parent_id ");
        sql.append(" WHERE model.bar_code = ? ");
        sql.append(" GROUP BY goods.name,goods.vender_Name,goods.vender_id,model.id,model.parent_id ,model.img_id,model.name,model.contour_id,model.contour_name,model.real_size,model.size,model.spec_id,model.spec_name ");
        try {
            List<GoodsModelInventoryScanDto> resultList = HQLConverterUtil.findVOListBySql(sql.toString(),new Object[]{barCode},GoodsModelInventoryScanDto.class,this.getEntityManager());
            return resultList.size() > 0 ? resultList.get(0) : null;
        } catch (Exception e) {
            throw ExceptionWapperUtil.wapper(e);
        }
    }
}
